package com.hzqy.service.epg;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hzqy.web.login.SessionKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.hzqy.commons.service.AuthenticatedService;
import com.hzqy.commons.utils.MD5;
import com.hzqy.web.vo.AcWinningVo;
import com.hzqy.web.vo.epg.AcProductsVo;

@Service
public class epgCommonService {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	//private AuthenticatedService authenticatedService;

	//----------------------------------------少儿----------------------------------------------------------------
	/**
	 * 少儿包月
	 */
	private final static int[] kid_month = {100088};
	/**
	 * 少儿线下，第三方---
	 */
	private  static int[] kid_ThirdLine = {701003,701007,701020,701021,711002,711004,90315443,90315444,90316080,701008,90401272,90401270,701010,90315025,701009,701031,701028};
	//----------------------------------------电视剧--------------------------------------------------------------
	/**
	 * 电视剧包月
	 *
	 */
	private final static int[] tv_month = {90311418,90312029,
			100096};

	/**
	 *电视剧 线下，第三方---
	 */
	private final static int[] tv_ThirdLine =  {700020,700045,700039,700040,711003,711001,
			90312030,90312395,90312394,71304360,90315442,90315445,90311417,//环球剧场
			711011,711012,711013,701011,701012,701019,701025,90400784,90400772,90400796,90400802,90400749,90400759//电视剧
	};
	//----------------------------------------电影----------------------------------------------------------------
	/**
	 * 电影包月
	 */
	private final static int[] mv_month = {90312853,100095};

	/**
	 * 电影 线下第三方---
	 */

	private final static int[] mv_ThirdLine = {700031,700032,700033,700053,90312852,90314661,90314662,90314663,90314761,90314440,700036,//环球影院
			711008,711009,711010,711017,711018,711026,90400714,90400722,90400745,90400744,90400748,90400747,701017,701018,701016,701026//电影

	};
	//----------------------------------------影视剧--------------------------------------------------------------
	/**
	 * 影视剧包月
	 */
	private final static int[] mtv_month = {100097,100106,660001,100110};
	/**
	 * 影视剧线下，第三方---
	 */
	private final static int[] mtv_ThirdLine = {701013,701014,701015,701024,701027,701029,701030,711014,711015,711016,711024,711027,711029,711030,//影视剧
			660003,100075,700037,700038,90311621,90310360,90314664,90314665,90314672,90313041,90310361,90311381,90311586,90312321,660002,100079,//超清
			90400840,90400845,90400846,908400841,908400842,90400843,90400844,90400847,90400850,90400851,90400911,90400857,90317640,90317641,90317642,90318200,90401183,90401182};
	//----------------------------------------合家欢--------------------------------------------------------------
	/**
	 * 合家欢
	 */
	private final static int[] filmAll = {100107,701032};
	//----------------------------------------NBA--------------------------------------------------------------
	/**
	 * NBA包月
	 */
	private final static int[] nba_month = {100093};
	/**
	 * NBA线下，第三方---
	 */
	private final static int[] nba_ThirdLine = {701022,701022};
	//----------------------------------------英超--------------------------------------------------------------
	/**
	 * 英超包月
	 */
	private final static int[] yc_month = {100094};
	/**
	 * 英超线下，第三方---
	 */
	private final static int[] yc_ThirdLine = {701006,701023};
	/**
	 * 超级体育高级会员包月ID，融合了nba、英超
	 */
	private final static int[] nba_yc = {100105};
	/**
	 * 音乐第三方
	 */
	private final static int[] music_ThirdLine = {704005,704006,704007,90400964,90400956};
	/**
	 * 音乐包月
	 */
	private final static int[] music_month = {400049,90400963};
	/**
	 * 戏迷天地
	 */
	private final static int[] xm_month = {100111};
	/**
	 * 戏迷天地第三方
	 **/
	private final static int[] xm_ThirdLine = {701035,701036,701037};


	public Map<String,Object> getOrderType(String userid,HttpServletRequest request,HttpServletResponse response) {
		Map<String,Object> map = new HashMap<String,Object>();
		HttpSession session = request.getSession();
		AuthenticatedService authenticatedService = new AuthenticatedService(request,response);
		if("".equals(userid)||"null".equals(userid)) {
			userid = String.valueOf(session.getAttribute(SessionKey.SESSION_SERVERID));
		}
		//String userID = String.valueOf(session.getAttribute(SessionKey.SESSION_SERVERID));
		String result = authenticatedService.getProductsOrdered(userid );
		if("-1".equals(result)){
			map.put("error", "用户"+userid  +"查询产品失败，接口未响应");
			return map;
		}
		JSONObject obj = JSONObject.parseObject(result);
		if("504".equals(obj.get("result").toString())) {
			map.put("result", "504");
			LOG.debug("用户："+userid  +" 未订购产品");
		}else if("0".equals(obj.get("result").toString())){
			List<AcProductsVo> acpv= JSONObject.parseArray(obj.get("products").toString(),AcProductsVo.class);
			map.put("result", "0");
			map.put("list",acpv);
			map.put("kid_month", resultProduct(kid_month,acpv));
			map.put("kid_ThirdLine", resultProduct(kid_ThirdLine,acpv));
			map.put("mv_month", resultProduct(mv_month,acpv));
			map.put("mv_ThirdLine", resultProduct(mv_ThirdLine,acpv));
			map.put("tv_month", resultProduct(tv_month,acpv));
			map.put("tv_ThirdLine", resultProduct(tv_ThirdLine,acpv));
			map.put("mtv_month", resultProduct(mtv_month,acpv));
			map.put("mtv_ThirdLine", resultProduct(mtv_ThirdLine,acpv));
			map.put("filmAll", resultProduct(filmAll,acpv));
			map.put("nba_month", resultProduct(nba_month,acpv));
			map.put("nba_ThirdLine", resultProduct(nba_ThirdLine,acpv));
			map.put("yc_month", resultProduct(yc_month,acpv));
			map.put("yc_ThirdLine", resultProduct(yc_ThirdLine,acpv));
			map.put("nba_yc", resultProduct(nba_yc,acpv));
			map.put("music_ThirdLine",resultProduct(music_ThirdLine,acpv));
			map.put("music_month",resultProduct(music_month,acpv));
			map.put("xm_month",resultProduct(xm_month,acpv));
			map.put("xm_ThirdLine",resultProduct(xm_ThirdLine,acpv));
			LOG.debug("用户"+userid  +"查询产品成功");
		}else {
			map.put("error", "用户"+userid  +"查询产品失败，接口未响应");
			LOG.debug("用户"+userid  +"查询产品失败，接口未响应");
		}
		return map;
	}

	private String resultProduct(int[] pro,List<AcProductsVo> acpv) {
		String result = "504";
		boolean flag = false;
		for(int i=0;i<acpv.size();i++) {
			String product = acpv.get(i).getProductId();
			if(flag) {
				break;
			}
			for(int j=0;j<pro.length;j++) {
				if(product.equals(String.valueOf(pro[j]))) {
					result = "0";
					LOG.debug(String.valueOf(pro[j]));
					flag = true;
					break;
				}
			}
		}
		return result;
	}

}
